package com.housesales.web;

import java.sql.SQLException;

import module.Employees;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import bussinessService.employeesBussinessService;
import bussinessService.impl.EmployeesBussinessServiceImpl;
import domain.vo.EmployeesQueryObject;
import domain.vo.Message;

/**
 * 首页
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/")
@SessionAttributes({"e_sn","shopId","groupId","e_name"}) 
public class Login {
    private @Value("#{sysProperties.privateUserNumber}") String privateUserNumber;
	/**
	 * logger
	 */
	private Logger logger = Logger.getLogger(Login.class.getName());

	/**
	 * 员工业务层实现类
	 */
	private employeesBussinessService empBIS = new EmployeesBussinessServiceImpl();
	/**
	 * 员工查询对象实例
	 */
	private EmployeesQueryObject employeeQueryObject = new EmployeesQueryObject();
	
	@RequestMapping("/index")
	public String index() {
		return "/index";
	}

	@RequestMapping(value = "login") 
	public ModelAndView login(@RequestParam("e_sn") String e_sn, @RequestParam("e_pass") String e_pass) {
		ModelAndView mv = new ModelAndView();
		try {
			logger.debug(privateUserNumber);
			employeeQueryObject.setE_sn(e_sn);
			employeeQueryObject.setE_pass(e_pass);
			employeeQueryObject.setE_status(Employees.ACTIVITY);
			boolean  passed = empBIS.validateUserExist(employeeQueryObject);
			if (passed) {
				Employees emp = empBIS.getById(e_sn);
				mv.addObject("e_sn", e_sn);
				mv.addObject("groupId", emp.getG_id());
				mv.addObject("shopId", emp.getShopId());
				mv.addObject("e_name", emp.getE_name());
				mv.setViewName("redirect:entryUrlJson.html");
			} else {
				mv.setViewName("redirect:responseMessageJson.html");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
			return mv;
		
	}
	
	@RequestMapping("responseMessageJson") 
	public @ResponseBody Message responseMessageJson() {
		logger.debug("返回主页面显示错误信息");
		Message message = new Message();
		message.setErrorMsg("用户名或密码错误");
		return message;
	}
	
	@RequestMapping(value = "entryUrlJson") 
	public @ResponseBody Message entryURLJson() {
		Message message = new Message();
		message.setUrl("manager.html");
		return message;
	}
	

	@RequestMapping(value = "manager") 
	public ModelAndView manager() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/admin/admin");
		return mv;
	}
}
